/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.NodeNameDisplayEditPart;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.diagram.usecase.figure.UseCaseStringFigure;
import com.cea.papyrus.diagram.usecase.helper.NodeUseCaseHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Property;

/**
 * Controller for a UseCase name display element
 */
public class UseCaseNameDisplayEditPart extends NodeNameDisplayEditPart {

	/**
	 * Creates a new UseCaseNameDisplayEditPart
	 * @param graphnode the graph node displaying the name of the use case
	 * @param modelManager the model manager associated to the editor
	 */
	public UseCaseNameDisplayEditPart(GraphNode graphnode, ModelManager modelManager) {
		super(graphnode,modelManager);
	}

	/**
	 * Creates a new UseCaseNameDisplayEditPart, with a given element whose name is displayed
	 * @param graphnode the graph node displaying the name of the use case
	 * @param element the named element whose name is displayed
	 * @param modelManager the model manager associated to the editor
	 */
	public UseCaseNameDisplayEditPart(GraphNode graphnode,org.eclipse.uml2.uml.Element element,ModelManager modelManager) {
		super(graphnode,element,modelManager);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.NodeNameDisplayEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		Dimension size = getFigure().getPreferredSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);
		UseCaseStringFigure fig = (UseCaseStringFigure)getFigure();

		if (getUmlElement() instanceof NamedElement) {
			fig.setText(((NamedElement)getUmlElement()).getName());
		}

		GraphNode parentGN = (GraphNode)getGraphNode().getSemanticParent();
		if (parentGN != null){
			if (!parentGN.getProperty().isEmpty()) {
				Property propertyAppearance = parentGN.getProperty(NodeHelper.NODE_FIGURE_APPEARANCE);
				if ( propertyAppearance != null ) {

					if ( !isParentShapeFigure(parentGN) 
							&& (propertyAppearance.getValue().equals(NodeUseCaseHelper.USECASE_ELLIPSE_FORM_NAME_OUTSIDE)
									|| (hasExtensionPoints(parentGN) && propertyAppearance.getValue().equals(NodeUseCaseHelper.USECASE_ELLIPSE_FORM))) ) {			
						fig.refreshColors(parentGN);
						fig.setVisible(true);
					} else{
						fig.setVisible(false);
					}					
				}
			}
		}

		((GraphicalEditPart) getParent()).setLayoutConstraint(this,	fig,r);
	}

	/**
	 * Checks if is parent is a shape figure.
	 * @param parentGN the parent graphnode
	 * @return <code>true</code>, if is parent is a shape figure
	 */
	private boolean isParentShapeFigure(GraphNode parentGN) {
		boolean isParentShapeFigure = false;
		if (parentGN.getSemanticModel() != null) {
			isParentShapeFigure = parentGN.getSemanticModel().getPresentation().equals(ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION);
		}
		return isParentShapeFigure;
	}

	/**
	 * Checks if the specified parent graph node has extension points.
	 * @param parentGN the parent graphnode to check 
	 * @return <code>true</code>, if the parent graphnode has extension points
	 */
	private boolean hasExtensionPoints(GraphNode parentGN) {
		boolean showExtensionPoints = false;
		if (parentGN.getContained() != null){
			GraphNode extensionPointList = (GraphNode) parentGN.getContained().get(0);	
			showExtensionPoints = !(extensionPointList.getContained().isEmpty());
		}
		return showExtensionPoints;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		return new UseCaseStringFigure(((NamedElement)getUmlElement()).getName());
	}
}